package com.quma.statistics;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class MysqlConnection {
	private static MysqlConnection instance;
	private  static Connection conn = null;
	private MysqlConnection(){}
	
	
	public static Statement getStatement(){
		if(conn == null){
			Exception e = new Exception("you need to call init method first");
			try {
				throw e;
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		try {
			return conn.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void init(String host,int port ,String dbName,String username,String psw){
		String url = "jdbc:mysql://" + host + ":"+ port + "/"+dbName+"?"
                + "user="+username+"&password=" + psw + "&useUnicode=true&characterEncoding=UTF8";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
